package com.rerun.phone.memberuser;

import com.rerun.common.anno.ResponseExceptionReturn;
import com.rerun.common.bean.Rest;
import com.rerun.common.controller.SuperController;
import com.rerun.entity.EduBackground;
import com.rerun.entity.MemberUser;
import com.rerun.entity.vo.EduBackgroundVo;
import com.rerun.service.IEduBackgroundService;
import com.rerun.service.IMemberUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Objects;

/**
 * The type Edu background controller.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /9/30 16:20
 */
@Api(value = "求职端-教育背景", description = "求职端-教育背景")
@RestController
@RequestMapping("/memberApi/eduBackground")
public class EduBackgroundController extends SuperController {
    @Resource
    private IEduBackgroundService iEduBackgroundService;
    @Resource
    private IMemberUserService iMemberUserService;

    /**
     * 根据memberId查询教育背景.
     *
     * @param memberId the member id
     * @return the rest
     */
    @ApiOperation(value = "查询教育背景", httpMethod = "POST", response = Rest.class, notes = "根据memberId教育背景")
    @RequestMapping(value = "/get/{memberId}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest findByMemberId(@PathVariable("memberId") String memberId) {
        return Rest.okData(iEduBackgroundService.selectByMemberId(memberId));
    }

    /**
     * 添加教育背景.
     *
     * @param eduBackgroundVo the edu background vo
     * @return the rest
     */
    @ApiOperation(value = "添加教育背景", httpMethod = "POST", response = Rest.class, notes = "添加教育背景")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest add(@ModelAttribute EduBackgroundVo eduBackgroundVo) {
        EduBackground eduBackground = new EduBackground();
        try {
            BeanUtils.copyProperties(eduBackground, eduBackgroundVo);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        eduBackground.setCreateTime(new Date());
        String memberId = eduBackgroundVo.getMemberId();

        insertPhoneByMemberId(eduBackground, memberId);

        boolean flag = iEduBackgroundService.insert(eduBackground);
        return flag ? Rest.ok() : Rest.failure("添加失败");
    }

    /**
     * 修改教育背景.
     *
     * @param eduBackgroundVo the edu background vo
     * @param id              the id
     * @return the rest
     */
    @ApiOperation(value = "修改教育背景", httpMethod = "POST", response = Rest.class, notes = "修改教育背景")
    @RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest edit(@ModelAttribute EduBackgroundVo eduBackgroundVo, @PathVariable("id") String id) {
        EduBackground eduBackground = new EduBackground();
        try {
            BeanUtils.copyProperties(eduBackground, eduBackgroundVo);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        eduBackground.setId(id);
        eduBackground.setUpdateTime(new Date());
        String memberId = eduBackgroundVo.getMemberId();

        insertPhoneByMemberId(eduBackground, memberId);

        boolean flag = iEduBackgroundService.updateById(eduBackground);
        return flag ? Rest.ok() : Rest.failure("修改失败");
    }

    /**
     * 通过教育背景ID删除教育背景.
     *
     * @param id the id
     * @return the rest
     */
    @ApiOperation(value = "通过教育背景ID删除教育背景", httpMethod = "POST", response = Rest.class, notes = "通过教育背景ID删除教育背景")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseExceptionReturn
    public Rest deleteById(@PathVariable("id") String id) {
        boolean flag = iEduBackgroundService.deleteById(id);
        return flag ? Rest.ok() : Rest.failure("删除失败");
    }


    /**
     * 同步手机号
     *
     * @param eduBackground 教育背景实体
     * @param memberId      用户ID
     * @see com.rerun.phone.memberuser.EduBackgroundController#add(EduBackgroundVo) {@link #edit(EduBackgroundVo, String)}
     */
    private void insertPhoneByMemberId(EduBackground eduBackground, String memberId) {
        MemberUser memberUser = iMemberUserService.selectById(memberId);
        if (Objects.nonNull(memberUser)) {
            String phone = memberUser.getPhone();
            eduBackground.setPhoneNumber(phone);
        }
    }
}
